
import 'package:flutter/material.dart';
import 'package:yp/helper.dart';
import 'package:yp/router.dart';
import 'package:get/get.dart';

import 'logic.dart';
import 'state.dart';

class SignInPage extends StatefulWidget {
  
  @override
  State<StatefulWidget> createState() {
    return _SignInPage();
  }
}

class _SignInPage extends State<SignInPage> {
  final SignInLogic logic = Get.put(SignInLogic());
  final SignInState state = Get.find<SignInLogic>().state;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        centerTitle: true,
        elevation: 0,
        backgroundColor: Color(0xffEA5504),
        title: Text(
          '签到',
          style: TextStyle(
            color: Colors.white,
            fontSize: 17,
          ),
        ),
        iconTheme: IconThemeData(
          color: Colors.white,
        ),
      ),
      body: GetBuilder<SignInLogic>(
        builder: (logic) {
          bool whetherSignIn = false;
          if (state.nextSevenDays.length > 0) {
            Map<String, dynamic> map = state.nextSevenDays[0];
            if (map['is_activated'] == 1) {
              whetherSignIn = true;
            }
          }
          return Container(
            color: Color(0xffF5F5F5),
            width: MediaQuery.of(context).size.width,
            child: ListView(
              children: [
                Stack(
                  children: [
                    Container(
                      color: Colors.white,
                      child: Column(
                        children: [
                          Container(
                            height: setWidth(476),
                            width: MediaQuery.of(context).size.width,
                            decoration: BoxDecoration(
                              image: DecorationImage(
                                image: AssetImage("assets/images/signInBg_image.png"),
                                fit: BoxFit.fill,
                              ),
                            ),
                          ),
                          SizedBox(
                            height: setWidth(358),
                          ),
                        ],
                      ),
                    ),
                    Container(
                      width: MediaQuery.of(context).size.width,
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.center,
                        children: [
                          Padding(
                            padding: EdgeInsets.only(
                              top: setWidth(82),
                              bottom: setWidth(50),
                            ),
                            child: InkWell(
                              onTap: () {
                                print('积分记录 点击事件！！！');
                                Get.toNamed(RouteConfig.myPoints);
                              },
                              child: Text.rich(
                                TextSpan(
                                  children: [
                                    TextSpan(
                                      text: '总积分：',
                                      style: TextStyle(
                                        color: Colors.white,
                                        fontSize: 16,
                                      ),
                                    ),
                                    TextSpan(
                                      // text: '${state.integralStr} ',
                                      text: parsePrice(state.integralStr),
                                      style: TextStyle(
                                        color: Colors.white,
                                        fontSize: 21,
                                      ),
                                    ),
                                    WidgetSpan(
                                      child: Column(
                                        children: [
                                          Image.asset(
                                            'assets/images/icon-left.png',
                                            width: setWidth(10),
                                            height: setWidth(26),
                                            color: Colors.white,
                                            fit: BoxFit.cover,
                                          ),
                                          SizedBox(
                                            height: setWidth(8),
                                          ),
                                        ],
                                      ),
                                    ),
                                  ],
                                ),
                              ),
                            ),
                          ),
                          InkWell(
                            onTap: () {
                              print('签到领积分 按钮点击事件！！！');
                              if (state.isSignIn == false) {
                                if (!whetherSignIn) {
                                  logic.signInClick(context);
                                } else {
                                  toast('亲!您今天已经签到了,不能再重复签到!');
                                  state.isSignIn = false;
                                }
                                state.isSignIn = true;
                              }
                            },
                            child: Container(
                              width: setWidth(350),
                              height: setWidth(106),
                              decoration: BoxDecoration(
                                image: DecorationImage(
                                  image: AssetImage('assets/images/signInBun_image.png'),
                                  fit: BoxFit.fill,
                                ),
                              ),
                              alignment: Alignment.center,
                              child: Column(
                                mainAxisAlignment: MainAxisAlignment.center,
                                children: [
                                  Text(
                                    whetherSignIn ? '今日已签' : '签到领积分',
                                    style: TextStyle(
                                      color: whetherSignIn ? Color(0x4D805321) : Color(0xff805321),
                                      fontSize: 20,
                                      fontWeight: FontWeight.bold,
                                    ),
                                  ),
                                  SizedBox(
                                    height: setWidth(5),
                                  ),
                                ],
                              ),
                            ),
                          ),
                          SizedBox(
                            height: setWidth(66),
                          ),
                          Container(
                            height: setWidth(414),
                            width: setWidth(680),
                            decoration: BoxDecoration(
                              color: Color(0xffeed8a4),
                              borderRadius: BorderRadius.circular(setWidth(34)),
                            ),
                            child: Column(
                              children: [
                                Padding(
                                  padding: EdgeInsets.only(
                                    top: setWidth(56),
                                    bottom: setWidth(52),
                                  ),
                                  child: Text(
                                    '已连续签到${state.continuitySignInDaysStr}天！', //'已连续签到1天，再签2天可有惊喜！',
                                    style: TextStyle(
                                      color: Colors.white,
                                      fontSize: 18,
                                    ),
                                  ),
                                ),
                                Container(
                                  height: setWidth(180),
                                  child: Stack(
                                    children: [
                                      Positioned(
                                        left: 0,
                                        top: setWidth(96),
                                        child: Container(
                                          height: setWidth(6),
                                          width: setWidth(680),
                                          color: Color(0xffe4c27c),
                                        ),
                                      ),
                                      Row(
                                        mainAxisAlignment: MainAxisAlignment.spaceAround,
                                        children: state.nextSevenDays.asMap().map((key, value) {
                                          Map<String, dynamic> map = value;
                                          print('+++++++++++   map = $map');
                                          return MapEntry(
                                            key,
                                            _singInItemWidget(
                                              imageStr: map['is_bonus'] == 1 ? 'signInGift_image.png' : '', // 为1时则显示礼品图样
                                              jiFenBool: map['is_activated'] == 1 ? true : false,
                                              jiFenStr: '+${map['sign_in_integral']}',
                                              titleStr: '${map['current_day_count']}天',
                                            ),
                                          );
                                        }).values.toList(),
                                      ),
                                    ],
                                  ),
                                ),
                              ],
                            ),
                          ),
                        ],
                      ),
                    ),
                  ],
                ),
                SizedBox(
                  height: setWidth(20),
                ),
                Container(
                  padding: EdgeInsets.only(
                    left: setWidth(40),
                    top: setWidth(24),
                    right: setWidth(40),
                    bottom: setWidth(60),
                  ),
                  color: Colors.white,
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Row(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          Container(
                            width: setWidth(240),
                            height: setWidth(4),
                            color: Color(0xffea5504).withOpacity(0.1),
                          ),
                          Padding(
                            padding: EdgeInsets.symmetric(horizontal: setWidth(14)),
                            child: Text(
                              '活动规则',
                              style: TextStyle(
                                color: Color(0xffEA5504),
                                fontSize: 16,
                              ),
                            ),
                          ),
                          Container(
                            width: setWidth(240),
                            height: setWidth(4),
                            color: Color(0xffea5504).withOpacity(0.1),
                          ),
                        ],
                      ),
                      SizedBox(
                        height: setWidth(24),
                      ),
                      Text(
                        state.signInRulesString,
                        // '1、每个账户每天签到一次，首日签到可领10积分，连续第二天可领20积分，第三天30积分，最多每天可获得50积分，连续签满7天后，可重新开始签到；\n'
                        //     '2、连续签到满3天、5天、7天可额外获得神秘礼物一份；\n'
                        //     '3、活动期间连续签到每天一次获得10、20、30、10、20、10、20积分；\n'
                        //     '4、以连续签到7天为一个周期，如果中途断签，记录累计天数自动清零；\n'
                        //     '5、本互殴的最终解释权在法律允许范围内归激荡说有，一旦发现恶意刷取积分行为，激荡有权收回积分，并取消或收回利用积分参与活动获得的任何奖励；\n'
                        //     '6、如有疑问，请联系客服',
                        style: TextStyle(
                          color: Color(0xff333333),
                          fontSize: 12,
                          height: 1.6,
                        ),
                      ),
                    ],
                  ),
                ),
              ],
            ),
          );
        },
      ),
    );
  }

  // signInItem
  Widget _singInItemWidget({
    String imageStr,
    String titleStr,
    String jiFenStr,
    bool jiFenBool,
  }) {
    return Container(
      width: setWidth(52),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Container(
            width: setWidth(52),
            height: setWidth(62),
            // color: Color(0xffe4c27c),
            child: imageStr == '' ? SizedBox() : Image.asset(
              'assets/images/$imageStr',
              width: setWidth(52),
              height: setWidth(62),
              fit: BoxFit.cover,
            ),
          ),
          Padding(
            padding: EdgeInsets.only(
              top: setWidth(12),
              bottom: setWidth(8),
            ),
            child: Container(
              width: setWidth(52),
              height: setWidth(52),
              decoration: BoxDecoration(
                color: jiFenBool ? Color(0xffC99F4F) : Color(0xffE4C27C),
                borderRadius: BorderRadius.circular(setWidth(26)),
              ),
              alignment: Alignment.center,
              child: jiFenBool ? Icon(
                Icons.check,
                size: setWidth(35),
                color: Colors.white,
              ) : Text(
                jiFenStr,
                textAlign: TextAlign.center,
                style: TextStyle(
                  color: Color(0xffF8E0A8),
                  fontSize: 10,
                ),
              ),
            ),
          ),
          Text(
            titleStr,
            style: TextStyle(
              color: Colors.white,
              fontSize: 14,
            ),
          ),
        ],
      ),
    );
  }

  // 弹框
  _shareShow() {
    return showDialog(
      context: context,
      useSafeArea: false,
      builder: (context) {
        return Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Container(
              width: setWidth(540),
              height: setWidth(380),
              decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(setWidth(24)),
                gradient: LinearGradient(
                  begin: Alignment.topCenter,
                  end: Alignment.bottomCenter,
                  colors: [
                    Color(0xffFDF0D9),
                    Color(0xffffffff),
                  ],
                ),
              ),
              child: Stack(
                children: [
                  Container(
                    width: setWidth(540),
                    height: setWidth(380),
                    decoration: BoxDecoration(
                      borderRadius: BorderRadius.circular(setWidth(24)),
                    ),
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.center,
                      children: [
                        Padding(
                          padding: EdgeInsets.only(
                            top: setWidth(36),
                            bottom: setWidth(16),
                          ),
                          child: Text(
                            '签到成功！',
                            style: TextStyle(
                              color: Color(0xff805321),
                              fontSize: 20,
                            ),
                          ),
                        ),
                        Container(
                          width: setWidth(144),
                          height: setWidth(144),
                          decoration: BoxDecoration(
                            color: Colors.yellow,
                            borderRadius: BorderRadius.circular(setWidth(72)),
                          ),
                          alignment: Alignment.center,
                          child: Text(
                            '+10',
                            style: TextStyle(
                              color: Color(0xff805321),
                              fontSize: 24,
                            ),
                          ),
                        ),
                        SizedBox(
                          height: setWidth(40),
                        ),
                        Text(
                          '恭喜获得10个积分～',
                          style: TextStyle(
                            color: Color(0xff999999),
                            fontSize: 14,
                          ),
                        ),
                      ],
                    ),
                  ),
                  Positioned(
                    top: 0,
                    right: 0,
                    child: InkWell(
                      onTap: () {
                        print('！！！删除弹框 点击事件！！！');
                        Navigator.pop(context);
                      },
                      child: Container(
                        padding: EdgeInsets.symmetric(horizontal: setWidth(28), vertical: setWidth(24)),
                        child: Image.asset(
                          'assets/images/close_images.png',
                          width: setWidth(26),
                          height: setWidth(26),
                          color: Color(0xff979797),
                          fit: BoxFit.cover,
                        ),
                      ),
                    ),
                  ),
                ],
              ),
            ),
            SizedBox(
              height: setWidth(60),
            ),
          ],
        );
      },
    );
  }


}